package com.example.shell;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.PlaybackParams;
import android.net.Uri;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.FileDescriptor;
import java.io.IOException;
import java.lang.IllegalStateException;
import java.util.Map;

public class WebMediaPlayer extends MediaPlayer {
  private static final String TAG = "WebMediaPlayer";
  private SurfaceView mSurfaceView = null;
  private MediaPlayer mPlayer = null;
  public WebMediaPlayer(SurfaceView view) {
    Log.i(TAG, "WebMediaPlayer");
    mSurfaceView = view;
    mPlayer = new MediaPlayer();
    if (mSurfaceView != null)
      mPlayer.setDisplay(mSurfaceView.getHolder());
  }

  @Override
  public void setPlaybackParams(PlaybackParams params) {
  }

  @Override
  public PlaybackParams getPlaybackParams() {
    return null;
  }

  @Override
  public void prepareAsync() {
    mPlayer.prepareAsync();
  }

  @Override
  public boolean isPlaying() {
    if (mPlayer == null)
      return false;
    return mPlayer.isPlaying();
  }

  @Override
  public int getCurrentPosition() {
    if (mPlayer == null)
      return 0;
    return mPlayer.getCurrentPosition();
  }

  @Override
  public int getDuration() {
    return mPlayer.getDuration();
  }

  @Override
  public void release() {
    mPlayer.release();
  }

  @Override
  public void setVolume(float leftVolume, float rightVolume) {
    try {
      mPlayer.setVolume(leftVolume, rightVolume);
    } catch(IllegalStateException e) {
      Log.e(TAG, e.toString());
    }
  }

  @Override
  public void start() {
    mPlayer.start();
  }

  @Override
  public void pause() {
    mPlayer.pause();
  }

  @Override
  public void seekTo(int msec) {
    mPlayer.seekTo(msec);
  }

  @Override
  public void setDataSource(Context context, Uri uri, Map<String, String> headers) throws IOException {
    mPlayer.setDataSource(context, uri, headers);
  }

  @Override
  public void setDataSource(FileDescriptor fd, long offset, long length) throws IOException {
    mPlayer.setDataSource(fd, offset, length);
  }

  @Override
  public void setDataSource(Context context, Uri uri) throws IOException {
    mPlayer.setDataSource(context, uri);
  }

  @Override
  public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) {
    mPlayer.setOnCompletionListener(listener);
  }

  @Override
  public void setOnErrorListener(MediaPlayer.OnErrorListener listener) {
    mPlayer.setOnErrorListener(listener);
  }

  @Override
  public void setOnPreparedListener(MediaPlayer.OnPreparedListener listener) {
    mPlayer.setOnPreparedListener(listener);
  }

  @Override
  public void setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener listener) {
    mPlayer.setOnVideoSizeChangedListener(listener);
  }
}

